package com.zwh.controller;

import com.zwh.entity.Admin;
import com.zwh.entity.PageDTO;
import com.zwh.service.AdminService;
import com.zwh.vcode.SecurityCode;
import com.zwh.vcode.SecurityImage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller("adminController")
@RequestMapping("admin")
public class AdminController {
	@Resource
	private AdminService adminService;

	@RequestMapping("queryAdmin")
	@ResponseBody
	public PageDTO<Admin> queryAdmin(int page,int rows){
		return adminService.selectAdmin(rows, page);
	}
	
	@RequestMapping("login")
	public String login(String username,String password,HttpSession session,String code,HttpServletRequest request){
		Admin admin = adminService.selectAdminByNP(username, password);
		//获取验证码并对比
		String vcode =(String) session.getAttribute("code");
		if(vcode.equalsIgnoreCase(code)){
			if(admin!=null){
				session.setAttribute("admin", admin);
				return "redirect:/main.jsp";
			}else{
				session.setAttribute("errno", -200);
				session.setAttribute("error_msg", "用户名或密码错误！");
				return "redirect:/login.jsp";
			}
		}else{
			session.setAttribute("error_msg", "验证码错误！");
			return "redirect:/login.jsp";
		}
	}
	
	@RequestMapping("updatePower")
	@ResponseBody
	public String updatePower(int power,int id){
		adminService.updatePower(id, power);
		return "";
	}
	

	@RequestMapping("updatePassword")
	@ResponseBody
	public String updatePassword(int id,String password){
		adminService.updatePassword(id, password);
		return "";
	}
	@RequestMapping("exit")
	@ResponseBody
	public String exit(HttpSession session){
		session.removeAttribute("login");
		session.removeAttribute("admin");
		return "";
	}
	
	@RequestMapping("deleteAdmin")
	@ResponseBody
	public String deleteAdmin(int id){
		adminService.deleteAdmin(id);
		return "";
	}
	
	//批量删除
	/*@RequestMapping("deleteBanners")
	@ResponseBody
	public String deleteBanners(Integer[] ids){
		bannerService.deleteBanners(ids);
		return "";
	}
	*/
	@RequestMapping("addAdmin")
	@ResponseBody
	public String addAdmin(MultipartFile file,HttpSession session,Admin admin) throws IllegalStateException, IOException{
		
		//String fileName = file.getOriginalFilename();
		//String realPath = session.getServletContext().getRealPath("/image/admin");
		//File destFile = new File(realPath,file.getOriginalFilename());
		file.transferTo(new File(session.getServletContext().getRealPath("/image/admin"),file.getOriginalFilename()));
		
		if(admin.getPower()==null){
			admin.setPower(0);
		}
		admin.setPower(0);
		admin.setImg_path("admin/"+file.getOriginalFilename());
		adminService.addAdmin(admin);
	   /* String json = JSON.toJSONString(queryAdmin(1,100));
		GoEasy goEasy = new GoEasy( "http://rest-hangzhou.goeasy.io", "BC-2feeb1426a254fbcb1ba0a48fc02dc18");
	    goEasy.publish("my_channel",json);*/
		return "";
	}
	//生成验证码
	@RequestMapping("vcode")
	public void vcode(HttpServletResponse response,HttpSession session) throws IllegalStateException, IOException{
		//生成随机数
		String code = SecurityCode.getSecurityCode();
		session.setAttribute("code",code);
		/*//用生成的随机数生成图片
		BufferedImage createImage = SecurityImage.createImage(code);
		//把图片通过流打印
		ServletOutputStream outputStream = response.getOutputStream();*/
		ImageIO.write(SecurityImage.createImage(code), "JPG", response.getOutputStream());
		
	}
}
